"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClassroomMiddleware = exports.createErrorMiddleware = exports.testParams = void 0;
const log4js_1 = require("log4js");
const defines_1 = require("../lib/defines");
const Classrooms_1 = require("../lib/Classrooms");
const socket_message_1 = require("../utils/socket_message");
const utils_1 = require("../utils/utils");
const logger = (0, log4js_1.getLogger)('CreateHelper');
function assertError(condition, errorMessage) {
    return condition && errorMessage;
}
function roomNotFind(room) {
    return assertError(!room || !room.classId, `params: { classId, classname } must required! 
     but it's ${(0, utils_1.stringify)(room)}`);
}
function roomNotInParams({ room, ...params }) {
    return !room && (`params: { room, ... } must required!
     but it's ${(0, utils_1.stringify)({ room, ...params })}`);
}
function membersNotInParams({ members, ...params }) {
    return (!members || !Array.isArray(members)) && (`params: { members, ... } must required!
     but it's ${(0, utils_1.stringify)({ members, ...params })}`);
}
function memberNotInParams({ member, ...params } = {}) {
    return !member && (`params: { member, ... } must required!
     but it's ${(0, utils_1.stringify)({ member, ...params })}`);
}
function membersNotFind(members) {
    return (!members || !Array.isArray(members)) && (`params: members:IMember[] must required!
     but it's ${(0, utils_1.stringify)(members)}`);
}
function memberNotFind(member) {
    return !member && (`params: { userId, username } must required!
     but it's ${(0, utils_1.stringify)(member)}`);
}
function testHasClassroom(classId) {
    let classroom = Classrooms_1.Classrooms.get(classId);
    return !classroom && 'room already created! please replace "classId".';
}
function testParams(params, cb = utils_1.noop) {
    const error = (message) => (0, socket_message_1.errorMsg)(defines_1.RequestMethod.create, message);
    const { room, mediaOptions } = params;
    let errorMessage = roomNotInParams(params) || membersNotInParams(params);
    if (!mediaOptions) {
        return cb && cb(error(`params: mediaOptions ${(0, utils_1.stringify)(mediaOptions)} must required!`));
    }
    if (errorMessage) {
        return cb(error(errorMessage));
    }
    const alreadyCreatedErrorMessage = testHasClassroom(room.classId);
    if (alreadyCreatedErrorMessage) {
        return cb(error(alreadyCreatedErrorMessage));
    }
}
exports.testParams = testParams;
async function createErrorMiddleware(app, io) {
    const { socket } = app.locals;
    const { userId, username } = socket.handshake.query;
    const owner = { userId, username };
    const error = (message) => (0, socket_message_1.errorMsg)(defines_1.RequestMethod.create, message, { owner });
    socket.use(async (event, next) => {
        const [name, params, cb] = event;
        if (name === defines_1.RequestMethod.create) {
            const { room, mediaOptions, members } = params;
            if (!room) {
                return cb && cb(error(`params: room ${(0, utils_1.stringify)(room)} must required!`));
            }
            if (!members || !Array.isArray(members)) {
                return cb && cb(error(`params: member[] ${(0, utils_1.stringify)(members)} must required!`));
            }
            if (!mediaOptions) {
                return cb && cb(error(`params: mediaOptions ${(0, utils_1.stringify)(mediaOptions)} must required!`));
            }
            let classroom = Classrooms_1.Classrooms.get(room.classId);
            if (classroom) {
                return cb && cb(error('room already created! please replace "classId".'));
            }
        }
        next();
    });
}
exports.createErrorMiddleware = createErrorMiddleware;
async function createClassroomMiddleware(app, io) {
    const { socket } = app.locals;
    const { userId, username } = socket.handshake.query;
    const error = (message) => (0, socket_message_1.errorMsg)(defines_1.RequestMethod.offer, message);
    logger.debug('------------------- createClassroomMiddleware -----------------------');
    socket.use(async (event, next) => {
        const [name, params, cb] = event;
        if (name === defines_1.RequestMethod.create) {
            const { room, mediaOptions, members } = params;
            let classroom = new Classrooms_1.Room({
                ...room,
                creator: { userId, username },
                classId: room.classId,
                classname: room.classname,
                classType: room.classType,
                inviteMode: room.inviteMode,
                joinMode: room.joinMode,
                chatMode: room.chatMode,
                conferenceMode: room.conferenceMode,
                socket,
            });
            classroom.setInviteMembers(members);
            classroom.setMediaOption(mediaOptions);
            Classrooms_1.Classrooms.set(room.classId, classroom);
        }
        next();
    });
}
exports.createClassroomMiddleware = createClassroomMiddleware;
